package demo.game.chess.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public enum EnumChessPiece implements Serializable {

	pawn(),

	rook(Arrays.asList(new ChessCell(0, 0), new ChessCell(0, 7 - 0),
			new ChessCell(7, 0), new ChessCell(7, 7 - 0))),

	knight(Arrays.asList(new ChessCell(0, 1), new ChessCell(0, 7 - 1),
			new ChessCell(7, 1), new ChessCell(7, 7 - 1))),

	bishop(Arrays.asList(new ChessCell(0, 2), new ChessCell(0, 7 - 2),
			new ChessCell(7, 2), new ChessCell(7, 7 - 2))),

	queen(Arrays.asList(new ChessCell(0, 3), new ChessCell(7, 3))),

	king(Arrays.asList(new ChessCell(0, 4), new ChessCell(7, 4)));

	final List<ChessCell> initialCells;

	/**
	 * for pawn
	 */
	private EnumChessPiece() {
		this.initialCells = new ArrayList<ChessCell>();
		for (int j = 0; j < 8; j++) {
			this.initialCells.add(new ChessCell(1, j));
			this.initialCells.add(new ChessCell(6, j));
		}

	}

	/**
	 * 
	 * @param initialCells initial cell in the top side.
	 */
	private EnumChessPiece(final List<ChessCell> initialCells) {
		this.initialCells = initialCells;
	}

	public AbstractChessPiece createInstance(final ChessCell initialPosition) {
		// not possible de use reflexion here.

		if (this == rook) {
			return new PieceRook(initialPosition);
		}
		if (this == knight) {
			return new PieceKnight(initialPosition);
		}
		if (this == bishop) {
			return new PieceBishop(initialPosition);
		}
		if (this == king) {
			return new PieceKing(initialPosition);
		}
		if (this == queen) {
			return new PieceQueen(initialPosition);
		}
		if (this == pawn) {
			return new PiecePawn(initialPosition);
		}
		throw new IllegalStateException("enum type invalide");
	}

	public boolean hasInitialPositionAt(final ChessCell p) {
		boolean result = this.initialCells.contains(p);
		//		if (result) {
		//			Log.debug(this + " is in " + p + "." + " Initial cell="
		//					+ this.initialCells);
		//		} else {
		//			Log.debug("\t\t" + this + " is NOT in " + p + "."
		//					+ " Initial cell=" + this.initialCells);
		//		}
		return result;
	}

	@Override
	public String toString() {
		return this.name().substring(0, 1).toUpperCase()
				+ this.name().substring(1);
	}

}
